package com.kiseny.service;

import java.util.List;

import com.common.intface.AuthenticationService;
import com.kiseny.dao.AccountDAO;
import com.kiseny.dao.RoleDAO;
import com.kiseny.inout.AuthenticationInput;
import com.kiseny.inout.AuthenticationOutput;
import com.kiseny.po.Account;

public class AuthenticationServiceImpl implements AuthenticationService {

    
    public AuthenticationOutput verifyAccount(AuthenticationInput input) {
        
        AuthenticationOutput output = new AuthenticationOutput();
        Account account = accountDAO.getAccountByName(input.getName());
        
        boolean passwordMatch = false;
        if (account != null) {
        	passwordMatch = (input.getPassword()).equals(account.getPassword());
            output.setValidFlg(passwordMatch);
            
            if (passwordMatch) {
            	account.setAddDefaultFlag(input.isAutoLoginFlag());
            	accountDAO.update(account);
            }
        } else {
            output.setValidFlg(false);
        }
        
        output.setAccount(account);
        
        return output;

    }
    
    public Account getAccountByName(String name) {
        
        return accountDAO.getAccountByName(name);
    }
    
    public Account checkAccount(String name, String password) {
        return accountDAO.checkAccount(name, password);
    }
    
    public void updateAccount(Account account) {
        accountDAO.update(account);
    }
    
    
    public List<Account> getAccountList() {
    	return accountDAO.findAll();
    }
    
    private AccountDAO accountDAO;
    private RoleDAO roleDAO;
    public AccountDAO getAccountDAO() {
        return accountDAO;
    }
    public void setAccountDAO(AccountDAO accountDAO) {
        this.accountDAO = accountDAO;
    }
    public RoleDAO getRoleDAO() {
        return roleDAO;
    }
    public void setRoleDAO(RoleDAO roleDAO) {
        this.roleDAO = roleDAO;
    }
}
